﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic.FileIO;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using SSISFromCode.Helpers;

namespace SSISFromCode
{

    internal interface IFlatFileColumnOptions
    {
        FieldType ColumnFieldType { get; }
        string Delimiter { get; }
        List<int> ColumnWidths { get; }
        bool TextQualified { get; }
        string Path { get; }
        bool ColumnNamesInFirstRow { get; }
    }
    
    /// <summary>
    /// Why do we need this class?  See comment above InitializeMetadata.
    /// </summary>
    internal class FlatFileColumnManager
    {
        private IFlatFileColumnOptions _columnOptions;
        public List<string> Columns { get; set; }

        public FlatFileColumnManager( IFlatFileColumnOptions columnOptions )
        {
            _columnOptions = columnOptions;
        }

        public void ExtractColumns()
        {
            Columns = new FlatFileColumnReader().Columns( _columnOptions );
        }

        /// <summary>
        /// See comment above PackageGenerator.ReinitializeMetadata; that's why we need to do this crap
        /// Also: http://social.msdn.microsoft.com/Forums/en-US/sqlintegrationservices/thread/b3d39c0b-f5f6-4047-a402-62ce5eadfd3e
        /// </summary>
        /// <param name="flatFileConnection"></param>
        /// <remarks>ExtractColumns() must have been called</remarks>
        public void InitializeMetadata( IDTSConnectionManagerFlatFile100 flatFileConnection )
        {
            for ( int i = 0; i < Columns.Count; i++ )
            {
                /// This object (IDTSConnectionManagerFlatFileColumn100) is not supposed to be referenced by my code according to doc:
                /// http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.dts.runtime.wrapper.idtsconnectionmanagerflatfilecolumn100.aspx
                var column = flatFileConnection.Columns.Add();
                /// Last column delimiter must be newline.
                /// If you select "," for the column delimiter in the designer for a Flat File Connection, and the row delimiter is newline, it does this same thing...
                column.ColumnDelimiter = ( i == Columns.Count - 1 ) ? DELIMITERS.NEWLINE : _columnOptions.Delimiter;
                column.TextQualified = _columnOptions.TextQualified;
                column.ColumnType = ( i == Columns.Count - 1 ) ? FieldType.Delimited.ToString() : _columnOptions.ColumnFieldType.ToString();  // last column is always delimited by end of line
                /// Here's one benefit of creating my own columns:
                /// My destination column in Sql Server is varchar.  The columns default to DT_WSTR (if you were using the UI to configure) which won't go into a varchar column w/o being
                /// manually changed or run through a data converter component.
                column.DataType = MISC.DEST_DATA_TYPE;
                column.DataPrecision = 0;
                column.DataScale = 0;
                if ( (_columnOptions.ColumnWidths != null) && (_columnOptions.ColumnWidths.Count > i) )
                {
                    column.ColumnWidth = _columnOptions.ColumnWidths[i];  // this ends up setting only InputColumWidth in the connection component
                }
                ( (IDTSName100)column ).Name = Columns[i];
            }
        }

    }
}
